package com.template.config.websocket;

import com.sun.security.auth.UserPrincipal;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

public class UserHandshakeHandler extends DefaultHandshakeHandler {
    @Override
    protected Principal determineUser(@NotNull ServerHttpRequest request,
                                      @NotNull WebSocketHandler wsHandler,
                                      Map<String, Object> attributes) {

        // 设置认证通过的用户到当前会话中
        return (UserPrincipal) attributes.get("user");
    }
}